package com.coldchain.admin.user.service.impl;

import com.coldchain.admin.user.entity.Admin;
import com.coldchain.admin.user.entity.CompanyAccount;
import com.coldchain.admin.user.mapper.CompanyAccountMapper;
import com.coldchain.admin.user.service.IAdminService;
import com.coldchain.common.util.MD5Utils;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.SystemModelEnum;
import org.apache.ibatis.builder.BuilderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private static Logger log= LoggerFactory.getLogger(CompanyServiceImpl.class);
    @Autowired
    private CompanyAccountMapper accountMapper;

    @Autowired
    private IAdminService adminService;


    public void modifyPassword(String oldPwd, String newPwd) throws BuilderException {
        String sysmodel= BaseContextHandler.getSysModl();
        if(sysmodel.equals(SystemModelEnum.USER)){
            CompanyAccount account=accountMapper.selectById(BaseContextHandler.getUserID());
            if(account==null){
                log.error("公司id不存在");
                throw new BuilderException("非法参数");
            }
            if(account.getPassword().equals(MD5Utils.getMD5Str(oldPwd))){
                account.setPassword(MD5Utils.getMD5Str(newPwd));
                accountMapper.updateById(account);
            }else {
                throw new BuilderException("密码错误");
            }
        }else  if(sysmodel.equals(SystemModelEnum.ADMIN)){
            Admin admin=adminService.getById(BaseContextHandler.getUserID());
            if(admin==null){
                log.error("用户不存在");
                throw new BuilderException("用户不存在");
            }
            if(admin.getLoginPwd().equals(MD5Utils.getMD5Str(oldPwd))){
                admin.setLoginPwd(MD5Utils.getMD5Str(newPwd));
                adminService.updateById(admin);
            }else {
                throw new BuilderException("密码错误");
            }
        }else {
            throw new BuilderException("非法参数");
        }
    }
}
